package com.cn.mmd.controller;

import com.cn.mmd.model.Course;
import com.cn.mmd.model.LabelIndex;
import com.cn.mmd.model.LabelTwoIndex;
import com.cn.mmd.model.Student;
import com.cn.mmd.service.CourseService;
import com.cn.mmd.service.EvaluateTeacherService;
import com.cn.mmd.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;

@Controller
/**
 * Created by Administrator on 2016/11/4.
 */
public class MajorCourseNumController {

    @Autowired
    StudentService studentService;
    @Autowired
    CourseService courseService;
    @Autowired
    EvaluateTeacherService evaluateTeacherService;

    /**
     *查找专业一周最大评价数量
     * @param studentMajor
     * @return
     */
    public synchronized int MaxEvaluateNum(String studentMajor,int weekNum){

        String studentRemarks="离开";
        int max=0;
        int MaxEvaluateNum=0;
        Student student=new Student();
        student.setStudentmajor(studentMajor);
        student.setStudentremarks(studentRemarks);
        List<Student> studentList=studentService.selectByMajor(student);
        int [] weekCourseNum=new int[25];
        for (int num=0;num<weekCourseNum.length;num++){
            weekCourseNum[num]=0;
        }
        if (studentList.size()>0){
            for (int a=0;a<studentList.size();a++){
                String className=studentList.get(a).getStudentclass();//班级
                System.out.println("得到的班级：" + className);

                if (className!=null){
                    List<Course> courseList=courseService.selectTeacherByClass(className);//课程
                    if(courseList.size()>0){

                        for (int b=0;b<courseList.size();b++){

                            String courseTime=courseList.get(b).getCoursetime();//课程时间
                            System.out.println("课程："+courseList.get(b).getCoursename()+"====课程时间："+courseTime);
                            if (courseTime!=null){
                                String [] courseTimes=courseTime.split(";");

                                for (int c=0;c<courseTimes.length;c++){
                                    System.out.println("课程时间分割后："+courseTimes[c]);
//                                    String cour=courseTimes[c].split("节")[1];
                                    int x=courseTimes[c].lastIndexOf("-");
                                    System.out.println("第二个‘_’的位置："+x);
                                    int subStr1 = Integer.parseInt(courseTimes[c].substring(courseTimes[c].lastIndexOf("第") + 1, courseTimes[c].indexOf("-")));
                                    int subStr2 = Integer.parseInt(courseTimes[c].substring(courseTimes[c].indexOf("-") + 1, courseTimes[c].lastIndexOf("周")));
                                    System.out.println("得到的周数：" + subStr1 + "----" + subStr2);
                                    for (int d=subStr1;d<subStr2;d++){
                                        weekCourseNum[d]=weekCourseNum[d]+1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

//            //这里得到的是该专业课程最多的那一周的课程数量   而不是当前周数的课程数量
//            for (int e=0;e<weekCourseNum.length;e++){
//                if (weekCourseNum[e]>max){
//                    max=weekCourseNum[e];
//                }
//            }
            //得到该专业要查的周数的课程数量
            max=weekCourseNum[weekNum];

            int studentNum=studentService.countLimitNum(student);
            System.out.println("该专业学生人数：" + studentNum);
            if (studentNum>0){
                MaxEvaluateNum=studentNum*max;
            }

        }else {
            //没查到学生
        }
        System.out.println("该专业传入周数评价数量：" + MaxEvaluateNum);
        return MaxEvaluateNum;
    }


    /**
     *计算标签评价得分（传入一个二级指标得分，加入统计计算总得分）
     */
    public synchronized Float EvaluateScore(List<LabelTwoIndex> labelTwoIndexList){

//        String indexScore="";
//        List<LabelIndex> labelIndexes = new ArrayList<LabelIndex>();
//        for (int l=0;l<labelTwoIndexList.size();l++){
//
//            List<Float> score = new ArrayList<Float>();
//            for (int m = labelTwoIndexList.size() - 1; m > l; m--) {
//                if (labelTwoIndexList.get(l).getIndexname().equals(labelTwoIndexList.get(m).getIndexname())) {
//                    score.add(labelTwoIndexList.get(m).getTwoindexscore());
//                    labelTwoIndexList.remove(m);
//
//                }
//            }
//            score.add(labelTwoIndexList.get(l).getTwoindexscore());
//            Float sum = 0f;
////            Float sum1 = 0f;
//            for (Float s : score) {
//                sum += s;
////                sum1 = (float) (Math.round(sum * 100)) / 100;
//            }
//            // System.out.println(labelTwoIndexList.get(l).getIndexname() + "-的和：" + sum1);//还未乘以权重的值
//
//            LabelIndex index=new LabelIndex();
//            index.setIndexname(labelTwoIndexList.get(l).getIndexname());
//            index.setPackagelabel(labelTwoIndexList.get(l).getPackagelabel());
//
//            LabelIndex labelIndex = evaluateTeacherService.selectIndex(index);
//
//            if (labelIndex != null) {
//                Float IndexScore = sum * labelIndex.getProportion();
//                LabelIndex labelIndex1 = new LabelIndex();  //乘以权重后的值
//                labelIndex1.setIndexname(labelIndex.getIndexname());
//                labelIndex1.setProportion(labelIndex.getProportion());
//                labelIndex1.setPackagelabel(labelIndex.getPackagelabel());
//                //labelIndex1.setIndexname(labelIndex.getIndexname());
//                labelIndex1.setIndexscore(IndexScore);
//
//                labelIndexes.add(labelIndex1);//存入一个list labelIndexes，里面全部为不同一级指标的得分
//            }
//
//        }
//
//        //算出这次评价的得分
//        Float sum = 0f;
//        for (LabelIndex s : labelIndexes) {
//            sum += s.getIndexscore();
//            sum = (float) (Math.round(sum * 100)) / 100;//这次评价的得分
//        }

//        map.put("sum",sum);
        float sum=0f;
        for (LabelTwoIndex labelTwoIndex:labelTwoIndexList){

            sum+=labelTwoIndex.getTwoindexscore();
        }

//        System.out.println("sum"+sum+"==="+"多少"+labelTwoIndexList.size());
        float avg=(float)(Math.round(sum/labelTwoIndexList.size() *100))/100;//算的是二级指标的平均分   没有评的就不算
        return avg;
    }


    /**
     *计算标签数量
     */
    public synchronized Map LabelNumber(String haveAlready,String evaluateLabel){
        Map<String,Object> map1=new HashMap<String, Object>();
        Map<String,Object> map2=null;
        List<Map> mapList=new ArrayList<Map>();

        //计算标签top
        String newLabel="";
        if (haveAlready!=null){

            System.out.println("原来已经有的："+haveAlready);
            String [] haveAlreadys=haveAlready.split("!!");//分解已有的标签
            String [] evaluateLabels=evaluateLabel.split(",");//分解此次评价的标签
            int labelNum=0;

            Map<String,Integer> map=new HashMap<String, Integer>();

            for (int a=0;a<evaluateLabels.length;a++){

                String label=evaluateLabels[a].split("!")[2];
                List<String> list=new ArrayList<String>();

                for (int b=0;b<haveAlreadys.length;b++){

                    String haveLabel=haveAlreadys[b].split("_")[0];//已有的标签
                    labelNum=Integer.parseInt(haveAlreadys[b].split("_")[1]);//已有的标签数量

                    list.add(haveLabel);
                    if (label.equals(haveLabel)){
                        labelNum=labelNum+1;

                        map.put(haveLabel,labelNum);

                    }else {

                        if (map.containsKey(haveLabel)){
                            if (map.get(haveLabel)<labelNum){
                                map.put(haveLabel,labelNum);
                            }
                        }else {
                            map.put(haveLabel,labelNum);
                        }
                    }
                }
                if (!list.contains(label)){

                    map.put(label,1);
                }
            }

            //遍历map
            for (Map.Entry<String,Integer> entry:map.entrySet()){
                if (newLabel.equals("")){
                    newLabel=entry.getKey()+"_"+entry.getValue();
                }else {
                    newLabel=newLabel+"!!"+entry.getKey()+"_"+entry.getValue();
                }
            }

        }else {
//            System.out.println("原来没有的："+evaluateLabel);
            String [] evaluateLabels=evaluateLabel.split(",");//分解此次评价的标签
            for (int a=0;a<evaluateLabels.length;a++){
                String label=evaluateLabels[a].split("!")[2];

                if (newLabel.equals("")){
                    newLabel=label+"_"+1;
                }else {
                    newLabel=newLabel+"!!"+label+"_"+1;
                }

            }
        }

        map1.put("newLabel",newLabel);
        return map1;
    }

}


